home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import re
- import string
- import posixpath
- from curses.ascii import isprint
- from checkbox.lib.bit import get_bitmask, test_bit
- from checkbox.lib.cache import cache
- from checkbox.lib.dmi import Dmi, DmiNotAvailable
- from checkbox.lib.input import Input
- from checkbox.lib.pci import Pci
- from checkbox.lib.usb import Usb
- from checkbox.properties import String
- from checkbox.registry import Registry
- from checkbox.registries.command import CommandRegistry
- from checkbox.registries.link import LinkRegistry
- from checkbox.registries.map import MapRegistry
-
- class DeviceRegistry(Registry):
-
- def __init__(self, environment, attributes):
- super(DeviceRegistry, self).__init__()
- self._environment = environment
- self._attributes = attributes
-
-
- def __str__(self):
- strings = _[1]
- return '\n'.join(strings)
-
-
- def _get_bus(self):
- sys_path = posixpath.join('/sys%s' % self._environment['DEVPATH'], 'subsystem')
- if posixpath.islink(sys_path):
- link = os.readlink(sys_path)
- if '/' in link:
- return posixpath.basename(link)
-
-
-
- def _get_category(self):
- if 'sys_vendor' in self._attributes:
- return 'SYSTEM'
- if 'IFINDEX' in self._environment:
- return 'NETWORK'
- if 'PCI_CLASS' in self._environment:
- pci_class = self._environment['PCI_CLASS']
- subclass_id = int(pci_class[-2:], 16)
- class_id = int(pci_class[:-2], 16)
- if class_id == Pci.BASE_CLASS_NETWORK:
- return 'NETWORK'
- if class_id == Pci.BASE_CLASS_DISPLAY:
- return 'VIDEO'
- if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_USB:
- return 'USB'
- if class_id == Pci.BASE_CLASS_COMMUNICATION and subclass_id == Pci.CLASS_COMMUNICATION_MODEM:
- return 'MODEM'
- if class_id == Pci.BASE_CLASS_INPUT and subclass_id == Pci.CLASS_INPUT_SCANNER:
- return 'SCANNER'
- if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_FIREWIRE:
- return 'FIREWIRE'
- if self._get_product_id():
- return 'OTHER'
-
-
- def _get_driver(self):
- if 'DRIVER' in self._environment:
- return self._environment['DRIVER']
- if 'ID_USB_DRIVER' in self._environment:
- return self._environment['ID_USB_DRIVER']
-
-
- def _get_path(self):
- return self._environment.get('DEVPATH')
-
-
- def _get_product_id(self):
- if 'PCI_ID' in self._environment:
- (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
- return int(product_id, 16)
- if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
- (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
- return int(product_id, 16)
- for attribute in ('idProduct', 'model_id'):
- if attribute in self._attributes:
- return int(self._attributes[attribute], 16)
-
-
-
- def _get_vendor_id(self):
- if 'PCI_ID' in self._environment:
- (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
- return int(vendor_id, 16)
- if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
- (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
- return int(vendor_id, 16)
- if 'idVendor' in self._attributes:
- return int(self._attributes['idVendor'], 16)
- vendor_id_path = posixpath.join(self._get_path(), '../vendor_id')
- if posixpath.exists(vendor_id_path):
- vendor_id = open(vendor_id_path, 'r').read().strip()
- return int(vendor_id, 16)
-
-
- def _get_subproduct_id(self):
- if 'PCI_SUBSYS_ID' in self._environment:
- pci_subsys_id = self._environment['PCI_SUBSYS_ID']
- (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
- return int(subproduct_id, 16)
-
-
- def _get_subvendor_id(self):
- if 'PCI_SUBSYS_ID' in self._environment:
- pci_subsys_id = self._environment['PCI_SUBSYS_ID']
- (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
- return int(subvendor_id, 16)
-
-
- def _get_product(self):
- for element in ('NAME', 'RFKILL_NAME', 'POWER_SUPPLY_MODEL_NAME'):
- if element in self._environment:
- return self._environment[element].strip('"')
-
- for attribute in ('description', 'model_name_kv', 'model', 'product_name'):
- if attribute in self._attributes:
- return self._attributes[attribute]
-
- bus = self._get_bus()
- if bus == 'sound':
- device = posixpath.basename(self._environment['DEVPATH'])
- match = re.match('(card|controlC|hwC|midiC)(?P<card>\\d+)', device)
- if match:
- card = match.group('card')
- in_card = False
- file = open('/proc/asound/cards', 'r')
- for line in file.readlines():
- line = line.strip()
- match = re.match('(?P<card>\\d+) \\[', line)
- if in_card:
- match = re.match('(?P<name>.*) at (?P<address>0x[%s]{8}) irq (?P<irq>\\d+)' % string.hexdigits, line)
- if match:
- return match.group('name')
- continue
- match
-
-
- path = None
- match = re.match('pcmC(?P<card>\\d+)D(?P<device>\\d+)(?P<type>\\w)', device)
- if match:
- path = '/proc/asound/card%s/pcm%s%c/info' % match.groups()
-
- match = re.match('(dsp|adsp|midi|amidi|audio|mixer)(?P<card>\\d+)?', device)
- if match:
- if not match.group('card'):
- pass
- card = 0
- path = '/proc/asound/card%s/pcm0p/info' % card
-
- if path and posixpath.exists(path):
- file = open(path, 'r')
- for line in file.readlines():
- match = re.match('name: (?P<name>.*)', line)
- if match:
- return match.group('name')
-
-
-
-
-
- def _get_vendor(self):
- if 'RFKILL_NAME' in self._environment:
- return None
- if 'POWER_SUPPLY_MANUFACTURER' in self._environment:
- return self._environment['POWER_SUPPLY_MANUFACTURER']
- if 'sys_vendor' in self._attributes:
- return self._attributes['sys_vendor']
- vendor_path = posixpath.join(self._get_path(), '../vendor_oui')
- if posixpath.exists(vendor_path):
- return open(vendor_path, 'r').read().strip()
-
-
- def items(self):
- return (('path', self._get_path()), ('bus', self._get_bus()), ('category', self._get_category()), ('driver', self._get_driver()), ('product_id', self._get_product_id()), ('vendor_id', self._get_vendor_id()), ('subproduct_id', self._get_subproduct_id()), ('subvendor_id', self._get_subvendor_id()), ('product', self._get_product()), ('vendor', self._get_vendor()), ('attributes', MapRegistry(self._attributes)), ('environment', MapRegistry(self._environment)), ('device', LinkRegistry(self)))
-
-
-
- class DmiDeviceRegistry(DeviceRegistry):
-
- def __init__(self, environment, attributes, category):
- super(DmiDeviceRegistry, self).__init__(environment, attributes)
- self._category = category
-
-
- def _get_category(self):
- return self._category
-
-
- def _get_path(self):
- path = super(DmiDeviceRegistry, self)._get_path()
- return posixpath.join(path, self._category.lower())
-
-
- def _get_product(self):
- if self._category == 'CHASSIS':
- type = int(self._attributes['chassis_type'])
- return Dmi.chassis_names[type]
- for name in ('name', 'version'):
- attribute = '%s_%s' % (self._category.lower(), name)
- product = self._attributes.get(attribute)
- if product and product != 'Not Available':
- return product
-
-
-
- def _get_vendor(self):
- attribute = '%s_vendor' % self._category.lower()
- if attribute in self._attributes:
- return self._attributes[attribute]
-
- _get_vendor = DmiNotAvailable(_get_vendor)
-
-
- class UdevRegistry(CommandRegistry):
- '''Registry for udev information.'''
- command = String(default = 'udevadm info --export-db')
-
- def _get_attributes(self, path):
- attributes = { }
- sys_path = '/sys%s' % path
-
- try:
- names = os.listdir(sys_path)
- except OSError:
- return attributes
-
- for name in names:
- name_path = posixpath.join(sys_path, name)
- if name[0] == '.' and name in ('dev', 'uevent') and posixpath.isdir(name_path) or posixpath.islink(name_path):
- continue
-
-
- try:
- value = open(name_path, 'r').read().strip()
- except IOError:
- continue
-
- value = value.split('\n')[0]
- attributes[name] = value
-
- return attributes
-
-
- def _ignore_device(self, device):
- if not device.bus:
- return True
- if not (device.product) and device.product_id is None:
- return True
- if (device.subproduct_id is None or device.subvendor_id is not None or device.subproduct_id is not None) and device.subvendor_id is None:
- return True
- if device.bus != 'dmi' and 'virtual' in device.path.split(posixpath.sep):
- return True
- return False
-
-
- def items(self):
- devices = []
- line_pattern = re.compile('(?P<key>\\w):\\s*(?P<value>.*)')
- multi_pattern = re.compile('(?P<key>\\w+)=(?P<value>.*)')
- for record in str(self).split('\n\n'):
- if not record:
- continue
-
- path = None
- environment = { }
- for line in record.split('\n'):
- match = line_pattern.match(line)
- if not match:
- raise Exception, 'Device line not supported: %s' % line
- match
- key = match.group('key')
- value = match.group('value')
- if key == 'P':
- path = value
- continue
- if key == 'E':
- match = multi_pattern.match(value)
- if not match:
- raise Exception, 'Device property not supported: %s' % value
- match
- environment[match.group('key')] = match.group('value')
- continue
-
- environment.setdefault('DEVPATH', path)
- attributes = self._get_attributes(path)
- if path == '/devices/virtual/dmi/id':
- device = DeviceRegistry(environment, attributes)
- devices.append(device)
- for category in ('BIOS', 'BOARD', 'CHASSIS'):
- device = DmiDeviceRegistry(environment, attributes, category)
- devices.append(device)
-
- device = DeviceRegistry(environment, attributes)
- devices.append(device)
-
- return _[1]
-
- items = cache(items)
-
- factory = UdevRegistry
-